layui.use(['table', 'jquery', 'layer'], function () {
    var table = layui.table,
        layer = layui.layer,
        $ = layui.jquery;
    var tableIns = table.render({
        elem: "#supplierLinkManList",
        url: ctx + "/supplier/linkManBySupplerId?supplierId=" + $("input[name ='supplierId']").val(),
        cellMinWidth: 80,
        height: "full-125",
        page: true,
        toolbar: "#toolbarDemo",
        cols: [[
            {type: "checkbox"},
            {type: "numbers"},
            {title: "姓名", field: "linkmanName"},
            {
                title: "性别", field: "linkmanSex", templet: function (data) {
                    if (data.linkmanSex == "0") {
                        return "男";
                    } else {
                        return "女"
                    }
                }
            }, {title: "联系方式", field: "linkmanPhone"},
            {title: "邮箱", field: "linkmanEmail"},
            {title: "职务", field: "linkmanJob"},
            {title: "创建时间", field: "linkmanCreateDate"},
            {title: "操作", templet: "#linkListBar"}
        ]]
    })

    table.on('toolbar(supplierLinkManFilter)', function (data) {


        if (data.event == "add") {
            openAddLinkMan();
        } else if (data.event == "del") {
            deleteLinkManById();
        }
    })

    table.on('tool(supplierLinkManFilter)', function (data) {
        if (data.event == "edit") {
            console.log(data)
            openAddLinkMan(data.data.id);

        }
    })

    function openAddLinkMan(id) {
        var title = "<h3>供应商管理---添加联系人</h3>"
        var url = ctx + "/supplier/toAddOrUpDateLinkMan?supplierId=" + $("[name ='supplierId']").val();
        if (id != null) {
            title = "<h3>供应商管理---更新联系人</h3>"
            url += "&&id=" + id;
        }
        layer.open({
            type: 2,
            title: title,
            area: ["650px", "400px"],
            content: url,
            shadeClose: true
        })
    }

    function deleteLinkManById() {
        var checkStatus = table.checkStatus("supplierLinkManList");
        var deleteLinkManData = checkStatus.data;
        if (deleteLinkManData.length < 1) {
            layer.msg("请选择记录", {icon: 5})
            return;
        }

        layer.confirm("确认删除联系人吗", {icon: 5, title: "联系人管理"}, function (index) {
            layer.close(index);
            var ids = "ids=";
            for (var i = 0; i < deleteLinkManData.length; i++) {
                if (i < deleteLinkManData.length - 1) {
                    ids = ids + deleteLinkManData[i].id + "&ids=";
                } else {
                    ids = ids + deleteLinkManData[i].id;
                }
            }

            $.ajax({
                type: "post",
                data: ids,
                url: ctx + "/supplier/deleteLinkManByIds",
                success: function (data) {
                    if (data.code == 200) {
                        layer.msg(data.msg, {icon: 6})
                        tableIns.reload();
                    } else {
                        layer.msg(data.msg)
                    }
                }, error: function (data) {
                    layer.msg(data.msg)
                }
            })

        })

    }
})